function padZero (num) {
	return num.toString().padStart(2, '0')
}

function formatDatetime (datetimeStr) {
	// 部分苹果设备不支持 new Date("2025-09-16 19:23:35")
	datetimeStr = datetimeStr.replace(/-/g, '/')
	const datetime = new Date(datetimeStr)
	let res = `${padZero(datetime.getMonth() + 1)}-${padZero(datetime.getDate())} ${padZero(datetime.getHours())}:${padZero(datetime.getMinutes())}`
	const now = new Date()
	return now.getFullYear() === datetime.getFullYear() ? res : datetime.getFullYear() + '-' + res
}

/**
 * 智能计算两个日期的时间差
 * @param {Date|string} startDate - 起始日期
 * @param {Date|string} endDate - 结束日期
 * @returns {string} 格式化后的时间差
 */
function formatDateDiff(startDate, endDate) {
	const start = new Date(startDate);
	const end = new Date(endDate);
	const diff = Math.abs(end - start);

	// 时间单位常量（毫秒）
	const SECOND = 1000;
	const MINUTE = 60 * SECOND;
	const HOUR = 60 * MINUTE;
	const DAY = 24 * HOUR;
	const MONTH = 30 * DAY; // 简化月份计算

	if (diff < MINUTE) {
		return '不足1分钟';
	} else if (diff < HOUR) {
		const minutes = Math.floor(diff / MINUTE);
		return `${minutes}分钟`;
	} else if (diff < DAY) {
		const hours = Math.floor(diff / HOUR);
		const mins = Math.floor((diff % HOUR) / MINUTE);
		return `${hours}小时${mins}分钟`;
	} else if (diff < MONTH) {
		const days = Math.floor(diff / DAY);
		const hours = Math.floor((diff % DAY) / HOUR);
		const mins = Math.floor((diff % HOUR) / MINUTE);
		return `${days}天${hours}小时${mins}分钟`;
	} else {
		const months = Math.floor(diff / MONTH);
		const days = Math.floor((diff % MONTH) / DAY);
		return `${months}个月${days}天`;
	}
}

export {
	padZero,
	formatDatetime,
	formatDateDiff
}